package com.wust.supermarket.controller;

import java.util.List;
import java.util.Map;

import org.apache.catalina.filters.AddDefaultCharsetFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wust.supermarket.domain.Member;
import com.wust.supermarket.domain.Merch;
import com.wust.supermarket.domain.Response;
import com.wust.supermarket.domain.Sale;
import com.wust.supermarket.service.SaleService;

@Controller
@RequestMapping("/sale")
public class SaleController {
	@Autowired
	private SaleService saleService;

	@PostMapping("/addlaji")
	@ResponseBody
	public Response addSalelaji(@RequestBody Map<String, Object> map) {
		int userId = Integer.parseInt((String) map.get("userId"));
		int merchId = Integer.parseInt((String) map.get("merchId"));
		int merchNumber = Integer.parseInt((String) map.get("merchNumber"));
		float salePrice = Float.parseFloat(((String) map.get("salePrice")));
		Merch merch = new Merch();
		merch.setMerchId(merchId);
		Member member = new Member();
		member.setMemberId(userId);
		Sale sale = new Sale();
		sale.setSaleId("0");
		sale.setMember(member);
		sale.setMerch(merch);
		sale.setMerchNumber(merchNumber);
		sale.setSalePrice(salePrice);
		System.out.println(sale);
		if(saleService.addlaji(sale))
			return new Response<String>(1, "添加成功", null);
		else {
			return new Response<String>(-1, "添加失败", null);
		}
	}

	@RequestMapping("/adds")
	public String Add() {
		return "sale/add";
	}

	// 获取所有订单
	@RequestMapping("/getAll")
	public String getAllSales(Model model) {
		List<Sale> sales = saleService.selectALlSales();
		model.addAttribute("sales", sales);
		return "sale/show";
	}

	// 通过订单id查订单
	@GetMapping("/getBySaleId")
	public List<Sale> getSalesBySaleId(String saleId) {
		return saleService.selectSaleById(saleId);
	}

	// 通过用户id查订单
	@GetMapping("/getByMemberId")
	public List<Sale> getSalesByMemberId(int memberId) {
		return saleService.selectSaleByMemberId(memberId);
	}

	// 通过商品id查订单
	@GetMapping("/getByMerchId")
	public List<Sale> getSalesByMerchId(int merchId) {
		return saleService.selectSaleByMerchId(merchId);
	}

	// 通过订单id,用户id,商品id查某条确定的订单
	@GetMapping("/getByDetail")
	public Sale getSaleByDetail(String saleId, int memberId, int merchId) {
		return saleService.selectSaleBySaleIdAndMemberIdAndMerchId(saleId, memberId, merchId);
	}

	// 添加订单
	@PostMapping("/add")
	public boolean addSale(@RequestBody List<Sale> sales) {
		return saleService.completeOneSale(sales);
	}

	// 修改订单（只能修改salePrice和merchNumber）
	@PostMapping("/change")
	public Response changeSale(@RequestBody Map<String, Object> map) {
		String saleId = (String) map.get("saleId");
		int memberId = Integer.parseInt((String) map.get("memberId"));
		int merchId = Integer.parseInt((String) map.get("merchId"));
		int merchNumber = Integer.parseInt((String) map.get("merchNumber"));
		float salePrice = Float.parseFloat(((String) map.get("salePrice")));
		Merch merch = new Merch();
		merch.setMerchId(merchId);
		Member member = new Member();
		member.setMemberId(memberId);
		Sale sale = new Sale();
		sale.setSaleId(saleId);
		sale.setMember(member);
		sale.setMerch(merch);
		sale.setMerchNumber(merchNumber);
		sale.setSalePrice(salePrice);
		System.out.println(sale);
		if (saleService.updateSale(sale))
			return new Response<String>(1, "修改成功", null);
		else {
			return new Response<String>(-1, "修改失败", null);
		}
	}

	// 删除一条确定的订单
	@PostMapping("/deleteOne")
	@ResponseBody
	public Response deleteSale(@RequestBody Map<String, Object> map) {
		String saleId = (String) map.get("saleId");
		int memberId = Integer.parseInt((String) map.get("memberId"));
		int merchId = Integer.parseInt((String) map.get("merchId"));
		System.out.println(saleId+memberId+merchId);
		if (saleService.deleteOneSaleLine(saleId, memberId, merchId))
			return new Response<String>(1, "删除成功", null);
		else {
			return new Response<String>(-1, "删除失败", null);
		}
	}

	// 删除某次交易的所有订单
	@PostMapping("/deleteBySaleId")
	public boolean deleteSalesBySaleId(String saleId) {
		return saleService.deleteSaleBySaleId(saleId);
	}

	// 删除某次交易中某商品的订单
	@PostMapping("/deletBySaleIdAndMerchId")
	public boolean deleteSalesBySaleIdAndMerchId(String saleId, int merchId) {
		return saleService.deleteSaleBySaleIdAndMerchId(saleId, merchId);
	}
	
	
	/**
	 * 统计销售额
	 */
	@GetMapping("/getAllSalePrice")
	@ResponseBody
	public float getAllSalePrice() {
		return saleService.getAllSalePrice();
	}
}
